package com.cfpamf.ms.insur.pay.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 获取http request 参数信息
 *
 * @author zhangnayi
 */
public class HttpRequestUtil {

    private HttpRequestUtil() {
    }

    /**
     * 获取http 请求头 user-agent
     *
     * @return
     */
    public static String getUserAgent() {
        return getRequest().getHeader("user-agent");
    }

    /**
     * 获取http 请求头 referer
     *
     * @return
     */
    public static String getReferer() {
        return getRequest().getHeader("referer");
    }

    /**
     * 获取requestUrl
     *
     * @return
     */
    public static String getRequestURI() {
        HttpServletRequest request = getRequest();
        if (request != null) {
            return getRequest().getRequestURI();
        }
        return "";
    }

    /**
     * 获取request请求所有参数
     *
     * @return
     */
    public static Map<String, String> getParameterMap() {
        Map<String, String> params = new HashMap<>();
        HttpServletRequest request = getRequest();
        if (request == null) {
            return params;
        }
        Enumeration<String> enums = getRequest().getParameterNames();
        while (enums.hasMoreElements()) {
            String paraName = enums.nextElement();
            params.put(paraName, getRequest().getParameter(paraName));
        }
        return params;
    }

    /**
     * 获取request请求所有参数
     *
     * @param request
     * @return
     */
    public static Map<String, String> getParameterMap(HttpServletRequest request) {
        Map<String, String> params = new HashMap<>();
        Enumeration<String> enums = request.getParameterNames();
        while (enums.hasMoreElements()) {
            String paraName = enums.nextElement();
            params.put(paraName, request.getParameter(paraName));
        }
        return params;
    }

    /**
     * 获取request请求所有参数
     *
     * @param request
     * @return
     */
    public static String getParameterJson(HttpServletRequest request) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String line = null;
        StringBuilder sb = new StringBuilder();
        try {
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * 获取客户端Id
     *
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        String[] ips = ip.split(",");
        if (ips.length > 0) {
            ip = ips[ips.length - 1];
        }
        return ip;
    }

    /**
     * 获取session
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * 获取一个header值
     *
     * @param headName
     * @return
     */
    public static String getHeader(String headName) {
        return getRequest().getHeader(headName);
    }
}
